package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.UpdateResult;
import com.tanhua.dubbo.server.enums.StatusEnum;
import com.tanhua.dubbo.server.enums.TopStatusEnum;
import com.tanhua.dubbo.server.pojo.Publish;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class MessagesApiImpl implements MessagesApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public String passPublish(List<Object> publishIds) {
        Query query =Query.query(Criteria.where("id").in(publishIds));
        Update update = Update.update("state",StatusEnum.PASS.getValue().toString());
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Publish.class);
        if(updateResult.getModifiedCount()>0){
            return "审批通过，成功！";
        }
        return null;
    }

    @Override
    public String rejectPublish(List<Object> publishIds) {
        Query query =Query.query(Criteria.where("id").in(publishIds));
        Update update = Update.update("state",StatusEnum.REJECTB.getValue().toString());
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Publish.class);
        if(updateResult.getModifiedCount()>0){
            return "审批拒绝，成功！";
        }
        return null;
    }


    @Override
    public String revocationPublish(List<Object> publishIds) {
        Query query =Query.query(Criteria.where("id").in(publishIds));
        Update update = Update.update("state",StatusEnum.UNREVIEWED.getValue().toString());
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Publish.class);
        if(updateResult.getModifiedCount()>0){
            return "消息撤销，成功！";
        }
        return null;
    }

    /**
     *
     * @param flag  1 未审核  2 审核通过 3 审核拒绝 4 审核过的动态
     * @return
     */
    @Override
    public List<Publish> queryPulishForState(List<ObjectId> publishIdList, Integer flag) {
        if (flag==1){
            //查所有待审核的动态
            Query query = Query.query(Criteria.where("state").in(StatusEnum.UNREVIEWED.getValue().toString()).and("id").in(publishIdList));
            return mongoTemplate.find(query, Publish.class);
        }
        if (flag==4){
            //撤销
            Query query = Query.query(Criteria.where("state").in(StatusEnum.PASS.getValue().toString(),StatusEnum.REJECTB.getValue().toString()).and("id").in(publishIdList));
            return mongoTemplate.find(query, Publish.class);
        }
        return null;
    }

    /**
     *
     * @param publishId
     * @param state 1未置顶 2置顶
     * @return
     */
    @Override
     public Publish queryPulishForTopState(String publishId, Integer state) {
        if (state==1){
            Query query = Query.query(Criteria.where("topState").is(TopStatusEnum.UNTOP.getValue()).and("id").is(publishId));
            return mongoTemplate.findOne(query, Publish.class);
        }
        if (state==2){
            Query query = Query.query(Criteria.where("topState").is(TopStatusEnum.TOP.getValue()).and("id").is(publishId));
            return mongoTemplate.findOne(query, Publish.class);
        }
        return null;
    }

    @Override
    public String topPublish(String publishId) {
        Query query =Query.query(Criteria.where("id").is(publishId));
        Update update = Update.update("topState",TopStatusEnum.TOP.getValue());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Publish.class);
        if(updateResult.getModifiedCount()>0){
            return "消息置顶，成功！";
        }
        return null;
    }

    @Override
    public String untopPublish(String publishId) {
        Query query =Query.query(Criteria.where("id").is(publishId));
        Update update = Update.update("topState",TopStatusEnum.UNTOP.getValue());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Publish.class);
        if(updateResult.getModifiedCount()>0){
            return "消息置顶，成功！";
        }
        return null;
    }

}
